<template>
  <div>
    <el-form label-position="right" label-width="150px" :model="writerForm" :rules="rules">
      <el-form-item label="数据库源：" prop="targetDatasourceId">
        <el-select v-model="writerForm.targetDatasourceId" filterable  @change="wDsChange">
          <el-option v-for="item in wDsList" :key="item.id" :label="item.sourceName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否创建表：" >
        <el-select v-model="writerForm.status" filterable  @change="changeType">
          <el-option v-for="item in dictList('sf_bool')" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <div style="margin: 5px 0;" />
      <el-form-item label="数据库表名：" v-show="writerForm.status">
        <el-checkbox v-model="writerForm.checkAll" :indeterminate="writerForm.isIndeterminate"
          @change="wHandleCheckAllChange">全选</el-checkbox>
        <div style="margin: 15px 0;" />
        <el-checkbox-group v-model="writerForm.tables" @change="wHandleCheckedChange">
          <el-checkbox v-for="c in wTbList" :key="c.id" :label="c">{{ c.tableName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as dsQueryApi from '@/api/dataextractionrealtime/metadata-query'
import { listDataSource } from '@/api/metadata/sqlconsole'
import { getTableList } from '@/api/masterdata/masterdataTable'
// import { list as jdbcDsList } from '@/api/dts/datax-jdbcDatasource'
import Bus from '../busWriter'
export default {
  name: 'TableWriter',
  props: ['readerInfo','writer','writeidList'],
  data() {
    return {
      jdbcDsQuery: {
        current: 1,
        size: 200,
        ascs: 'datasource_name'
      },
      wDsList: [],
      schemaList: [],
      fromTableName: '',
      wTbList: [],
      dataSource: '',
      createTableName: '',
      writerForm: {
        sourceId: undefined,
        tables: [],
        checkAll: false,
        isIndeterminate: true,
        tableSchema: '',
        status: 'false',
        sourceName:undefined,
        targetDatasourceId:''
      },
      readerForm: this.getReaderData(),
      rules: {
        targetDatasourceId: [{ required: true, message: '数据库源为必填项', trigger: 'change' }]

      },
      key: ''
    }
  },

  created() {
    this.getJdbcDs()
  },
  methods: {
    getHistory(){

      this.writerForm.targetDatasourceId=this.writer.sourceId
      this.writerForm.status=this.writer.status=='1'?'true':'false'
      console.log(this.writer)
      this.getTables()
    },
    rest(){
      this.wTbList=[]
     this.writerForm= {
        sourceId: undefined,
        tables: [],
        checkAll: false,
        isIndeterminate: true,
        tableSchema: '',
        status: 'false',
        sourceName:undefined
      }
    },
    //选择是否创建表
    changeType(e) {
      this.key = e
      if (e == 'true') {
        this.wTbList = this.readerInfo.tables
      } else {
        // 获取可用表
        this.getTables('writer')
      }
    },
    // 获取可用数据源
    getJdbcDs() {
      this.loading = true
      listDataSource().then(response => {
        this.wDsList = response.data.data
        this.loading = false
      })
    },
    // 获取表名
    getTables(type) {


        // 组装
        getTableList({ sourceId: this.writerForm.targetDatasourceId}).then(res => {
          if (res.data.code === 200) {
            this.wTbList = res.data.data
            if(this.writeidList.length>0){
                    let arr=[]
                    res.data.data.forEach(item=>{
                      this.writeidList.forEach(it=>{
                        if(item.id==it){
                         arr.push(item)
                        }
                      })

                    })
                    this.writerForm.tables=arr
                  }
          }
        })

    },
    getSchema() {
      const obj = {
        sourceId: this.writerForm.sourceId
      }
      getTableList(obj).then(res => {
        if (res.data.code === 200) {
          this.schemaList = res.data.data
        }
      })
    },
    wDsChange(e) {
      console.log(e)
      // 清空
      this.writerForm.tables = []
      this.writerForm.tableName = ''
      // this.writerForm.sourceId = e
      this.wDsList.find((item) => {
        if (item.id === e) {
          this.writerForm.sourceName=item.sourceName
          this.dataSource = item
        }
      })
      this.changeType(this.key)
      Bus.sourceId = e
      // this.$emit('selectDataSource', this.dataSource)

    },
    wHandleCheckAllChange(val) {
      this.writerForm.tables = val ? this.wTbList : []
      this.writerForm.isIndeterminate = false
    },
    wHandleCheckedChange(value) {
      const checkedCount = value.length
      this.writerForm.checkAll = checkedCount === this.wTbList.length
      this.writerForm.isIndeterminate = checkedCount > 0 && checkedCount < this.wTbList.length
    },
    getData() {
      if (Bus.sourceId) {
        this.writerForm.sourceId = Bus.SourceId
      }
      return this.writerForm
    },
    getReaderData() {
      return this.$parent.getReaderData()
    },
    getTableName() {
      return this.fromTableName
    },
    createTable() {
      const obj = {
        sourceId: this.writerForm.sourceId,
        tableName: this.createTableName
      }
      // dsQueryApi.createTable(obj).then(response => {
      //   this.$notify({
      //     title: 'Success',
      //     message: 'Create Table Successfully',
      //     type: 'success',
      //     duration: 2000
      //   })
      // }).catch(() => console.log('promise catch err'))
    }
  }
}
</script>
